package cn.user.domain.dao;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import cn.user.domain.User;


public class UserDao {

	private JdbcTemplate jdbctemplate = new JdbcTemplate(Druid0.getDataSource());

	public User login(User user) {
		try{
		String sql = "select * from user where username=? and  pass=?"; 
		User user0 = jdbctemplate.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), user.getUsername(),
		user.getPass());
		return user0;
		}
		catch(DataAccessException e) {
			//e.printStackTrace();
			return null;
		}
	}
	
	public boolean checkUsername(String name) {  //查询用户名以判断用户是否存在
		try{
		String sql="select * from user where username=?";
		User user0 = jdbctemplate.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), name);
		return false;
		}
		catch(DataAccessException e) {
			//e.printStackTrace();
			return true;
		}
	}
	
	
	public boolean register(User user) {
		String sql = "INSERT INTO USER VALUES(?,?,?,?,?,?,?,?)";
		int flag = jdbctemplate.update(sql,null,user.getUsername(),user.getPass(),user.getEmail(),user.getProvince(),user.getPhone_num()
				,user.getGender(),user.getBirthday());
		if(flag!=0)
			return true;
		return false;
	}

}
